import 'package:flutter/material.dart';
import 'package:flutterapp/common/base/base_provider.dart';

/// 数据处理模型类
///
/// 这个类的子类会进行数据请求，数据存储
///
abstract class BaseModel extends ChangeNotifier {
  // 是否为release版
  static bool get isRelease => bool.fromEnvironment("dart.vm.product");

  // 加载view的key
  GlobalKey<LoadingViewState> loadingViewKey = GlobalKey<LoadingViewState>();
  // 错误view的key
  GlobalKey<ErrorViewState> errorViewKey = GlobalKey<ErrorViewState>();

  /// 显示加载view
  showLoading([String msg = '加载中']) {
    loadingViewKey.currentState?.show(msg);
  }

  /// 隐藏加载view
  hideLoading() {
    loadingViewKey.currentState?.hide();
  }

  /// 显示错误view
  showError([String msg = '']) {
    errorViewKey.currentState?.show();
  }

  /// 隐藏错误view
  hideError() {
    errorViewKey.currentState?.hide();
  }

  /// 当前model是否已经销毁
  bool isDispose = false;

  /// 这是一个初始化方法，用于属性的初始化和数据获取
  Future<bool> init();

  @override
  void dispose() {
    super.dispose();

    isDispose = true;
  }

  @override
  void notifyListeners() {
    //当前model销毁了，则不会再去通知监听器刷新
    if (!isDispose) {
      super.notifyListeners();
    }
  }
}
